// pages/recovery/recovery.js
import Toast from '@vant/weapp/toast/toast';
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userAvatar:'',
        message:'是否将该卡复机',
        stopMessage:'停机后，该号卡将无法使用，是否进行停机',
        remainFlow:'20',
        remainMoney:'32',
        show:false,
        showStop:false,
        iccid:'',
        cardStatus:'',
        stopTime:'23/09/01',
        isStopServeice: true,// 是否该账号已经激活： true：已激活 false:已停机
        showLoading:false,
        getMsg:'',
        objCard:{},
        erorMessage:'',
        showError:false,
    },
    onLoad(options){
    var cardId = options.cardId;
    var that = this;
    this.selectComponent("#loading").show()
    app.getLogUser(function (uInfo) {
      const {
        userId,
        userAvatar
      } = uInfo;
      that.setData({
        userId: userId,
        cardId: cardId,
        userAvatar:userAvatar
      })
       that.getData(cardId)
    })
    },
    getData(cardId){
      var that = this;
      wx.request({
        url: app.globalData.apiUrlNew + 'Mini/BalUpdated.ashx?action=updown',
        data: {
          cardId: cardId
        },
        dataType: 'json',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          that.selectComponent("#loading").hide()
          var respd = res.data;
          console.log(respd)
          if (respd.getNum == 0) {
            that.setData({
              getMsg: respd.getMsg,
              cardStatus: respd.objCard.card_statusmsg,
              objCard:respd.objCard
            });
          } else {
            Toast.fail(respd.getMsg)
          }
        },
        fail(resfail) {
          that.selectComponent("#loading").hide()
          Toast.fail(resfail.data)
        }
      })
    },
    recoveryConfirm(){
        this.onRecovery()
    },
    stopConfirm(){
        this.onRecovery()
    },
    stopAccount(e){
      let status = e.currentTarget.dataset.status
      if(status == '已停用'){
        this.setData({show:true})
      }else{
        this.setData({showStop:true})
      }
    },
   onRecovery(){
      const that = this;
      wx.request({
        url: app.globalData.apiUrl + 'Mini/BalCard.ashx?action=16',
        data: {
          cardId: that.data.cardId,
          userId: that.data.userId,
          updown: that.data.cardStatus=='已停用'?'0':'1',
        },
        dataType: 'json',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(resp) {
          var respd = resp.data;
          console.log(respd);
          that.setData({
              showStop:false,
              show:false
            })
          if (respd.getNum == 0) {
            Toast.success('变更成功');
            wx.navigateBack({
              delta: 1
            });
          } else {
            that.setData({
              erorMessage:respd.getMsg,
              showError:true
            })
          }
        }
    })},
    sureBtn(){
      this.setData({
        showError:false
      })
      wx.navigateBack({
        delta: 1
      });
    }
})